<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>同学会</title>
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>

    <link rel="stylesheet" href="../js/libs/sui/sm.min.css">
    <link rel="stylesheet" href="../iconfont/iconfont.css">
    <link rel="stylesheet" href="../less/main.css"/>

    <script type='text/javascript' src='../js/libs/jquery-3.1.0.min.js' charset='utf-8'></script>
    <script type="text/javascript">
        var Zepto = jQuery;
    </script>
    <script type='text/javascript' src='../js/libs/sui/sm.js' charset='utf-8'></script>
    <script src="../js/libs/juicer-min.js"></script>
    <script src="../js/libs/jquery.md5.js"></script>
    <script src="../js/api.js"></script>
    <script src="../js/utils.js"></script>
    <script src="../js/common.js"></script>
</head>
<body>
<div class="content native-scroll bg-white p-t22">
    <!--同学会-->
    <div class="class-mates">
        <!-- 头部-->
        <div class="head-no-toolbar bg-self-green no-left-icon">
            <span class="icon iconfont c-white p-l" onclick="javascript:api.closeWin();">&#xe61f;</span>
            <span class="c-white t-center">同学会</span>
        </div>
        <!-- 班级群聊-->
        <div class="class-talk-title">
            <a id="class_qun_chat" progress="false" load="public/pages/Duer/class-talk.html" title="班级群聊">
                <img src="../images/portrait.jpg" alt=""/>
                <p>班级群聊</p>
            </a>
        </div>
        <div id="private-chat-list" class="self-talk-title">
            <!--<div class="item">-->
                <!--<img src="../images/portrait.jpg" alt=""/>-->
                <!--<p>神犬小七</p>-->
            <!--</div>-->
            <!--<div class="item">-->
                <!--<img src="../images/portrait.jpg" alt=""/>-->
                <!--<p>神犬小七</p>-->
            <!--</div>-->
            <!--<div class="item">-->
                <!--<img src="../images/portrait.jpg" alt=""/>-->
                <!--<p>神犬小七</p>-->
            <!--</div>-->
        </div>
    </div>
</div>
<script id="chat-tpl" type="text/template">
    {@each chatList as it,index}
    <div class="item item-chat" data-student-id="&{it.STUDENT_ID}" data-student-name="&{it.STUDENT_NAME}" data-student-header="&{it.PHOTO_PATH}">
        <img src="&{it.PHOTO_PATH}" />
        <p>&{it.STUDENT_NAME}</p>
    </div>
    {@/each}
</script>

<script>
    apiready = function () {
        juicer.set({
            'tag::operationOpen': '{@',
            'tag::operationClose': '}',
            'tag::interpolateOpen': '&{',
            'tag::interpolateClose': '}',
            'tag::noneencodeOpen': '$${',
            'tag::noneencodeClose': '}',
            'tag::commentOpen': '{#',
            'tag::commentClose': '}',
            'strip': false,
            'cache': false
        });

        // 登录用户
        var userInfo = getLoginUserInfo();
        // 学生信息
        var stuInfo = getLoginStudentInfo();
        // 融云 Token
        var rongcloudToken = null;
        // 班级 ID
        var tableId = api.pageParam.tableId;
        // 班级名称
        var tableName = api.pageParam.tableName;
        // 是否已获取获取用户：true-是；false-否
        var userFlag = false;
        // 同学会
        var classMates = new Array();

        if (isObject(stuInfo) && isNumber(tableId)) {
            var sql = "SELECT T1.PHOTO_PATH, T2.STUDENT_ID, T2.STUDENT_NAME FROM SU_STAFF T1, S_STUDENT T2, S_STUDENT_CLASS T3 WHERE T1.STAFF_ID = T2.STAFF_ID AND T2.STUDENT_ID = T3.STUDENT_ID AND T3.CLASS_ID = '{0}'"

            dbQuery(1, 10, sql.format(tableId), true, function (ret, err) {
                // 查询成功
                if (isObject(ret) && (ret.statusCode == 0)) {
                    userFlag = true;

                    var chatData = {
                        url : getUrl(),
                        chatList: ret.result
                    };

                    $.each(chatData.chatList, function(i, e) {
                        if (isEmpty(e.PHOTO_PATH)) {
                            e.PHOTO_PATH = "../images/default-header.png";
                        } else {
                            e.PHOTO_PATH = ("{0}upload/{1}".format(getUrl(), e.PHOTO_PATH));
                        }

                        var classMate = new Object();
                        classMate.STUDENT_ID = e.STUDENT_ID;
                        classMate.PHOTO_PATH = e.PHOTO_PATH;

                        classMates.push(classMate);
                    });

                    $("#private-chat-list").empty().html(juicer($("#chat-tpl").html(), chatData));
                } else {
                    userFlag = false;
                }
            });

            if (isEmpty(userInfo.PHOTO_PATH)) {
                userInfo.PHOTO_PATH = "../images/default-header.png";
            } else {
                userInfo.PHOTO_PATH = "{0}upload/{1}".format(getUrl(), userInfo.PHOTO_PATH);
            }

            // 获取融云 Token
            getRongcloudToken(stuInfo.STUDENT_ID, stuInfo.STUDENT_NAME, userInfo.PHOTO_PATH, function (ret, err) {
                // 获取 Token 成功
                if (isObject(ret) && (ret.code == 200)) {
                    rongcloudToken = ret.token;
                }
            });

            // 班级群聊
            $("#class_qun_chat").click(function() {
                if (userFlag) {
                    if (isNotEmpty(rongcloudToken)) {

                        openWin("index_qunliao", "index_qunliao.html", {
                            className : tableName,
                            classId : tableId,
                            classMates : classMates,
                            rongcloudToken : rongcloudToken
                        });
                    } else {
                        toast("尚未获取融云Token，稍后再试");
                    }
                } else {
                    toast("尚未获取同学信息，稍后再试");
                }
            });

            // 私聊
            $("#private-chat-list").on("click", ".item-chat", function() {
                var $this = $(this);

                if (isEmpty(rongcloudToken)) {
                    toast("尚未获取融云Token，稍后再试");
                } else {
                    openWin("index_danliao", "index_danliao.html", {
                        toStudentId : $this.data("studentId"),
                        toStudentName : $this.data("studentName"),
                        toStudentHeader : $this.data("studentHeader"),
                        rongcloudToken : rongcloudToken
                    });
                }
            });
        }
    };
</script>
</body>
</html>
